<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        .test-button {
            background: #667eea;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        .test-button:hover {
            background: #5a67d8;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            white-space: pre-wrap;
        }
        .error {
            background: #fecaca;
            color: #dc2626;
        }
        .success {
            background: #d1fae5;
            color: #065f46;
        }
    </style>
</head>
<body>
    <h1>API测试页面</h1>
    <p>用于测试后端API连接和功能</p>

    <div class="test-section">
        <h3>基础连接测试</h3>
        <button class="test-button" onclick="testHealth()">测试服务状态</button>
        <button class="test-button" onclick="testRoot()">测试根路径</button>
        <div id="health-result" class="result"></div>
    </div>

    <div class="test-section">
        <h3>会话管理测试</h3>
        <button class="test-button" onclick="testGetSessions()">获取会话列表</button>
        <button class="test-button" onclick="testCreateSession()">创建新会话</button>
        <button class="test-button" onclick="testDeleteSession()">删除会话</button>
        <div id="session-result" class="result"></div>
    </div>

    <div class="test-section">
        <h3>AI对话测试</h3>
        <input type="text" id="test-input" placeholder="输入测试消息" style="width: 300px; padding: 8px; margin-right: 10px;">
        <button class="test-button" onclick="testChat()">发送消息</button>
        <div id="chat-result" class="result"></div>
    </div>

    <script src="api.js"></script>
    <script>
        let currentTestSessionId = null;

        async function testHealth() {
            const resultDiv = document.getElementById('health-result');
            try {
                const response = await fetch('http://localhost:8000/health');
                const data = await response.json();
                resultDiv.textContent = JSON.stringify(data, null, 2);
                resultDiv.className = 'result success';
            } catch (error) {
                resultDiv.textContent = '连接失败: ' + error.message;
                resultDiv.className = 'result error';
            }
        }

        async function testRoot() {
            const resultDiv = document.getElementById('health-result');
            try {
                const response = await fetch('http://localhost:8000/');
                const data = await response.json();
                resultDiv.textContent = JSON.stringify(data, null, 2);
                resultDiv.className = 'result success';
            } catch (error) {
                resultDiv.textContent = '连接失败: ' + error.message;
                resultDiv.className = 'result error';
            }
        }

        async function testGetSessions() {
            const resultDiv = document.getElementById('session-result');
            try {
                const response = await SessionAPI.getSessions();
                resultDiv.textContent = JSON.stringify(response, null, 2);
                resultDiv.className = 'result success';
                
                if (response.code === 200 && response.data.sessions.length > 0) {
                    currentTestSessionId = response.data.sessions[0].session_id;
                }
            } catch (error) {
                resultDiv.textContent = '获取会话失败: ' + error.message;
                resultDiv.className = 'result error';
            }
        }

        async function testCreateSession() {
            const resultDiv = document.getElementById('session-result');
            try {
                const response = await SessionAPI.createSession();
                resultDiv.textContent = JSON.stringify(response, null, 2);
                resultDiv.className = 'result success';
                
                if (response.code === 200) {
                    currentTestSessionId = response.data.session_id;
                }
            } catch (error) {
                resultDiv.textContent = '创建会话失败: ' + error.message;
                resultDiv.className = 'result error';
            }
        }

        async function testDeleteSession() {
            const resultDiv = document.getElementById('session-result');
            if (!currentTestSessionId) {
                resultDiv.textContent = '请先创建或获取一个会话';
                resultDiv.className = 'result error';
                return;
            }
            
            try {
                const response = await SessionAPI.deleteSession(currentTestSessionId);
                resultDiv.textContent = JSON.stringify(response, null, 2);
                resultDiv.className = 'result success';
                currentTestSessionId = null;
            } catch (error) {
                resultDiv.textContent = '删除会话失败: ' + error.message;
                resultDiv.className = 'result error';
            }
        }

        async function testChat() {
            const resultDiv = document.getElementById('chat-result');
            const input = document.getElementById('test-input').value.trim();
            
            if (!input) {
                resultDiv.textContent = '请输入测试消息';
                resultDiv.className = 'result error';
                return;
            }
            
            if (!currentTestSessionId) {
                resultDiv.textContent = '请先创建或获取一个会话';
                resultDiv.className = 'result error';
                return;
            }
            
            try {
                const response = await ChatAPI.chat(input, currentTestSessionId);
                resultDiv.textContent = JSON.stringify(response, null, 2);
                resultDiv.className = 'result success';
            } catch (error) {
                resultDiv.textContent = '发送消息失败: ' + error.message;
                resultDiv.className = 'result error';
            }
        }
    </script>
</body>
</html> 